<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [基础控制器]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------

declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\exception\ValidateException;
use think\Validate;
use think\facade\View;

/**
 * 控制器基础类
 */
abstract class Base
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;
    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = ['app\admin\middleware\Admin'];

    /**
     * 构造方法
     * @access public
     * @param App $app 应用对象
     */
    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;

        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
        //面包导航
        $auth = new \Auth();
        $breadCrumb = $auth->getBreadCrumb();
        $breadCrumb = format_bread_crumb($breadCrumb);
        $action = request()->action();
        $system = get_system_config();
        View::assign(['system_config' => $system]);
        View::assign(['action' => $action]);
        View::assign(['breadCrumb' => $breadCrumb]);
    }

    /**
     * 验证数据
     * @access protected
     * @param array $data 数据
     * @param string|array $validate 验证器名或者验证规则数组
     * @param array $message 提示信息
     * @param bool $batch 是否批量验证
     * @return array|string|true
     * @throws ValidateException
     */
    protected function validate(array $data, $validate, array $message = [], bool $batch = false)
    {
        if (is_array($validate)) {
            $v = new Validate();
            $v->rule($validate);
        } else {
            if (strpos($validate, '.')) {
                // 支持场景
                list($validate, $scene) = explode('.', $validate);
            }
            $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
            $v = new $class();
            if (!empty($scene)) {
                $v->scene($scene);
            }
        }

        $v->message($message);
        // 是否批量验证
        if ($batch || $this->batchValidate) {
            $v->batch(true);
        }

        $result = $v->failException(false)->check($data);
        if (true !== $result) {
            return $v->getError();
        } else {
            return $result;
        }
    }

    //分页参数获取
    protected function formatPage(&$page, &$order, $default_sort = "create_time")
    {
        //分页
        $page = input('page') ? input('page') : 1;
        $pageSize = input('limit') ? input('limit') : config('pageSize');
        $page = ['list_rows' => $pageSize, 'page' => $page];

        //排序
        $sort_order = input('sort_order') ? input('sort_order') : 'desc';
        $order = input('sort_order') ? [input('sort_by') => $sort_order] : [$default_sort => $sort_order];

    }

    //分页数据返回格式
    protected function pageResult($list)
    {
        return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
    }

    protected function ajaxSuccess($data = [], $msg="操作成功"):array
    {
        return ['code'=>0, 'msg'=>$msg, 'data'=>$data];
    }
    protected function ajaxError($msg="操作失败"):array
    {
        return ['code'=>1, 'msg'=>$msg];
    }
}
